/*************************************************************************
	> File Name: word_count.cc
	> Author: likang
	> Mail:  
	> Created Time: 2014年12月21日 星期日 22时18分54秒
 ************************************************************************/

#include<iostream>
#include<fstream>
#include<map>
#include<string>

using namespace std;

void word_count (string file) {
	ifstream input(file.c_str());
	string s;
	map <string, int> word;
	while (input >> s) {

		/****
		 * there are two ways: 1.使用下标 2. 使用insert
		 * **/
		//1.
		pair<map<string, int>::iterator, bool> ret = word.insert(make_pair(s,1));
		if (!ret.second) 
			++ret.first->second;
		//2.
		//++word[s];
	}
	input.close();
	input.clear();
	ofstream output("output.txt");
	for (map <string, int>::iterator it = word.begin(); it != word.end(); ++it) {
		output << it->first << ":" << it->second <<endl;
	}
	output.close();
	output.clear();
}

int main (int args, char *argv[]) {
	if (args == 1) {
		cout << "please input file name " << endl;
	}
	else {
		word_count(argv[1]);
	}
	return 0;
}

